package com.ruoyi.service.impl;

import java.io.IOException;
import java.util.*;

import com.alipay.easysdk.factory.Factory;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.result.Result;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.domain.*;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.IT03Service;
import com.ruoyi.service.IT18Service;
import com.ruoyi.service.IT25Service;
import com.ruoyi.system.service.impl.SysRoleServiceImpl;
import com.ruoyi.uitity.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.T10Mapper;
import com.ruoyi.service.IT10Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 充值记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-29
 */
@Service
public class T10ServiceImpl implements IT10Service
{
    @Autowired
    private T10Mapper t10Mapper;
    @Autowired
    private IT18Service t18Service;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IT03Service t03Service;
    @Autowired
    private IT25Service t25Service;
    @Autowired
    private SysRoleServiceImpl sysRoleService;
    @Autowired
    private T06ServiceImpl t06ServiceImpl;

    /**
     * 查询充值记录
     *
     * @param F1001 充值记录主键
     * @return 充值记录
     */
    @Override
    public T10 selectT10ByF1001(Long F1001)
    {
        return t10Mapper.selectT10ByF1001(F1001);
    }

    /**
     * 查询充值记录列表
     *
     * @param t10 充值记录
     * @return 充值记录
     */
    @Override
    public List<T10> selectT10List(T10 t10)
    {
        PageUtils.startPage();
        return t10Mapper.selectT10List(t10);
    }

    /**
     * 新增充值记录
     *
     * @param t10 充值记录
     * @return 结果
     */
    @Override
    public int insertT10(T10 t10)
    {
        return t10Mapper.insertT10(t10);
    }

    /**
     * 修改充值记录
     *
     * @param t10 充值记录
     * @return 结果
     */
    @Override
    public int updateT10(T10 t10)
    {
        return t10Mapper.updateT10(t10);
    }

    /**
     * 批量删除充值记录
     *
     * @param F1001s 需要删除的充值记录主键
     * @return 结果
     */
    @Override
    public int deleteT10ByF1001s(Long[] F1001s)
    {
        return t10Mapper.deleteT10ByF1001s(F1001s);
    }

    /**
     * 删除充值记录信息
     *
     * @param F1001 充值记录主键
     * @return 结果
     */
    @Override
    public int deleteT10ByF1001(Long F1001)
    {
        return t10Mapper.deleteT10ByF1001(F1001);
    }

    @Override
    public List<T10VO> getPayRecords(Long F0301) {
        return t10Mapper.getPayRecords(F0301);
    }

    @Override
    public List<T10VO> getPAyRecordPeople(Long F0601) {
        return t10Mapper.getPAyRecordPeople(F0601);
    }

    @Override
    public List<T10> selectT10ComList(T10 t10, Long comId) {
        return t10Mapper.selectT10ComList(t10, comId);
    }

    @Override
    public Result recharge(HttpServletRequest request, Long money) {
        LoginUser loginUser =tokenService.getLoginUser(request);
        List<Long> longs = sysRoleService.selectRoleListByUserId(loginUser.getUserId());
        Long roleId = longs.get(0);
        T10 t10 = new T10();
        t10.setF1002(money);
        t10.setF1003(new Date());
        if(roleId==2L){
            //从微信小程序发过来的请求
            T06 t06 = t06ServiceImpl.selectT06ByF0601(loginUser.getUserId());
            t10.setF0301(t06.getF0301());
            t10.setF1006(0L);
            t10.setF1008(t06.getF0618());
            t10.setF0601(t06.getF0601());
            t10.setF1007(t06.getF0602());
        }else{
            T18 t18 = t18Service.selectT18ByF1801(loginUser.getUserId());
            Long comId = t18.getF0301();
            t10.setF0301(comId);
            t10.setF1008(t18.getF1808());
            t10.setF1007(t18.getF1804());
            t10.setF1006(1L);
        }
        t10.setF1010(1L);
        t10.setF1009(UUID.randomUUID().toString());
        t10Mapper.insertT10(t10);
        Alipay alipay = new Alipay();
        alipay.setTraceNo(t10.getF1009());
        alipay.setTotalAmount(Double.valueOf(t10.getF1002()));
        alipay.setSubject(UUID.randomUUID().toString());
        return Result.success(alipay,"充值初始化成功");
    }
    @Override
    @Transactional
    public Result rechargeCallback(HttpServletRequest request) throws Exception {
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("=========支付宝异步回调========");

            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
                 System.out.println(name + " = " + request.getParameter(name));
            }

            String tradeNo = params.get("out_trade_no");
            String gmtPayment = params.get("gmt_payment");
            String alipayTradeNo = params.get("trade_no");
            // 支付宝验签
            if (Factory.Payment.Common().verifyNotify(params)) {
                // 验签通过
                T10 t10 = t10Mapper.selectT10ByF1009(tradeNo);
                T25 t25 =t25Service.selectT25ByF2502(t10.getF1002());
                t10.setF1010(0L);
                if(ObjectUtils.isEmpty(t10.getF0601())){
                    T03 t03 = t03Service.selectT03ByF0301(t10.getF0301());
                    t10.setF1005(t03.getF0310()+ t25.getF2503());
                    t03.setF0310(t10.getF1005());
                    t03Service.updateT03(t03);
                }else{
                    T06 t06 = t06ServiceImpl.selectT06ByF0601(t10.getF0601());
                    t10.setF1005(t06.getF0611()+ t25.getF2503());
                    t06.setF0611(t10.getF1005());
                    if(ObjectUtils.isEmpty(t06.getF0606())){
                        t06.setF0606(0L);
                    }
                    t06.setF0606(t06.getF0606()+t10.getF1002());
                    T03 t03 = t03Service.selectT03ByF0301(t06.getF0301());
                    t03.setF0310(t03.getF0310()-t25.getF2503());
                    if(t03.getF0310()<0){
                        t03.setF0310(0L);
                    }
                    t03.setF0316(t03.getF0316()+t25.getF2502());
                    t03Service.updateT03(t03);
                    t06ServiceImpl.updateT06(t06);
                }
                t10Mapper.updateT10(t10);
            }
        }
        System.out.println("=========支付宝异步回调========成功");
        return Result.success("充值成功");
    }

    @Override
    public void excelExport(HttpServletResponse response) throws IOException {
        List<Map<String,Object>> list = t10Mapper.selectAllComPany();
        int sum = 0;
        for(Map<String,Object> map:list){
            sum+=Integer.parseInt(map.get("totalMoney").toString());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("totalMoney",sum);
        map.put("comPany","总计");
        list.add(map);
        String baseName = "收入总明细导出";
        String[] headers = {"序号","公司名","充值金额"};
        String[] engHeaders = {"index","comPany", "totalMoney"};
        ExcelUtils.exportXls(response, list, baseName, headers, engHeaders);
    }

    @Override
    public List<T10> selectT10ListNoPage(T10 t10) {
        return t10Mapper.selectT10List(t10);
    }

    @Override
    public List<T10> getDetail(HttpServletRequest request, T10 t10) {
        Long userId = tokenService.getLoginUser(request).getUserId();
        Long comId = t18Service.selectF0301ByF1801(userId);
        t10.setF0301(comId);
        t10.setF1006(0L);
        t10.setF1010(0L);
        PageUtils.startPage();
        return t10Mapper.selectT10List(t10);
    }

    public Long getTotalMoney() {
        return t10Mapper.getTotalMoney();
    }

    public List<Map<String, Object>> getTotalcz() {
        return t10Mapper.getTotalcz();
    }

    public Long getComTotal(Long F0301) {
        return t10Mapper.getComTotal(F0301);
    }

    public int getComMoney(Long compId) {
        return t10Mapper.getComMoney(compId);
    }
}
